package leetcode;

/**
 * 分发糖果
 */
public class Leetcode135 {
    public int candy(int[] ratings) {
        int size = ratings.length;
        if (size < 2) {
            return size;
        }
        int num[] = new int[size];
        for (int i = 0; i < size; i++) {
            num[i] = 1;
        }
        for (int i = 1; i < size; i++) {
            if (ratings[i] > ratings[i-1]) {
                num[i] = num[i - 1] + 1;
            }
        }
        for (int i = size - 1; i > 0; --i) {
            if (ratings[i] < ratings[i - 1]) {
                num[i-1] = Math.max(num[i-1], num[i] + 1);
            }
        }
        int sum = 0;
        for (int i : num) {
            sum += i;
        }
        return sum;
    }
}
